// component/shop-cart/shop-cart.ts

import { getSpecListBySpecId, getSpecDetailByPid, addShopCart } from '@/api/sale'
import { toast } from '@/utils/extendApi'
Component({
  properties: {
    //是否弹框
    show: {
      type: Boolean
    },
    //商品信息
    goodItem: {
      type: Object
    },
    // 是否有购买按钮
    ishaveBuy: {
      type: Boolean
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    specDetail: [], //类型按钮
    spec: [],
    shopCartData: {
      typeId: '',
      specId: '',
      goodsNum: 1, //类型按钮下标集合
      goodsTotalNum: 0,
      specDetailId: '', //获取已经选中的类型
      totalPrice: 0,
      specName: '',
      specDetailName: ''
    }, //购物车数据
    specIndex: []
  },
  /**
   * 组件的方法列表
   */
  methods: {
    //关闭弹框
    onClose() {
      this.setData({
        show: false
      })
    },
    //打开弹框赋值
    getAllData() {
      if (!this.properties.goodItem.typeId) return
      getSpecListBySpecId(this.properties.goodItem.typeId).then(res => {
        const data = res.data.map(item => {
          return {
            ...item,
            isActive: false,
            haveProduct: true
          };
        });
        const specIndex = res.data.map(item => {
          return {
            id: item.id,
            index: 0
          };
        });
        this.setData({
          spec: data,
          specIndex: specIndex
        })
      })
    },
    getSpecDetail(event: any) {
      getSpecDetailByPid(event.currentTarget.dataset.id).then(res => {
        const specIndex = res.data.map(item => {
          return {
            id: item.id,
            index: 0
          };
        });
        this.setData({
          specDetail: res.data,
          specIndex: specIndex,
          "shopCartData.specId": event.currentTarget.dataset.id,
          "shopCartData.specName": event.currentTarget.dataset.name
        })
      })
    },
    //添加购物车
    addShopCart() {
      if (!this.data.shopCartData || this.data.shopCartData.specId === '' || this.data.shopCartData.specDetailId === '') {
        toast({ title: "请选择类型" })
        return
      }
      const specDetailAll = { ...this.properties.goodItem, ...this.data.shopCartData, totalPrice: this.properties.goodItem.price * this.data.shopCartData.goodsNum, buyType: this.properties.goodItem.buyType.name ,goodsTotalNum: this.properties.goodItem.goodsNum }
      this.setData({
        show: false
      })
      addShopCart(specDetailAll).then(res => {
        toast({ title: '加入购物车成功' })

      })
    },
    //选中商品类型赋值
    bindSpecId(event: any) {
      const index = this.data.specIndex.filter(ss => ss.id === event.currentTarget.dataset.id)[0].index
      let specDetailId = event.currentTarget.dataset.id
      if (index % 2 === 0) {
        specDetailId = event.currentTarget.dataset.id
      } else {
        specDetailId = ""
      }
      this.data.specDetail[event.currentTarget.dataset.index].isActive = index % 2 === 0
      this.data.specIndex.filter(ss => ss.id === event.currentTarget.dataset.id)[0].index = index + 1
      this.setData({
        specDetail: this.properties.specDetail,
        specIndex: this.data.specIndex,
        "shopCartData.specDetailId": specDetailId,
        "shopCartData.specDetailName": event.currentTarget.dataset.name
      })
    },
    onNumChange(value: any) {
      let goodsNum = 0
      if (this.properties.goodItem.goodsNum < value.detail) {
        goodsNum = this.properties.goodItem.goodsNum
        toast({ title: "超出库存数量！" })
      } else {
        goodsNum = value.detail
      }
      this.setData({
        "shopCartData.goodsNum": goodsNum,
        "shopCartData.totalPrice": goodsNum * this.properties.goodItem.price
      })
    }
  }
})